import apiUrl from '@/api/apiUrl'
import {Toast, MessageBox} from 'mint-ui'

export default {
    chioseBtn () {
        this.$set(this, 'popupVisible', true)
    },
    setData () {
        if (this.$route.query.user_name) {
            this.$set(this, 'user_name', this.$route.query.user_name)
        }
        let params = {
            params: {
                page: 1,
                row: 1000,
                type: 2
            }
        }
        this.$http.get(apiUrl.admin.users, params).then(res => {
            if (res.data.code === 0) {
                this.$set(this, 'users', this.users.concat(res.data.result))
            } else {
                Toast(res.data.msg)
            }
        }).catch(err => {
            console.log(err)
        })
    },
    chioseItem (e) {
        this.$set(this, 'chiose', e.target.getElementsByClassName('mint-cell-text')[0].innerHTML)
    },
    cannel () {
        this.$set(this, 'chiose', '')
        this.$set(this, 'popupVisible', false)
    },
    chioseRoom () {
        this.$set(this, 'user_name', this.chiose)
        this.$set(this, 'popupVisible', false)
    },
    submit () {
        this.loading = true
        this.validator.validateAll({
            room_name: this.room_name,
            password: this.password,
            odds: this.odds,
            goldRatio: this.goldRatio,
            endTime: this.endTime,
            link: this.link,
            user_name: this.user_name,
        }).then((result) => {
            if (result) {
                this.todo()
            } else {
                this.error()
            }
        });
    },
    todo () {
        let params = new URLSearchParams()
        params.append('user_name', this.user_name)
        params.append('password', this.password)
        params.append('room_name', this.room_name)
        params.append('odds', this.odds)
        params.append('gold_ratio', this.goldRatio)
        params.append('end_time', this.endTime)
        params.append('link', this.link)
        this.$http.post(apiUrl.admin.createRoom, params).then(result => {
            if (result.data.code !== 0) {
                this.error(result.data.msg)
            } else {
                this.$set(this, 'loading', false)
                MessageBox('提示', '房间创建成功');
                this.$router.push({name: 'admin-room-lists'})
            }
        }).catch(err => {
            console.log(err)
        })
    },
    error (msg = null) {
        let instance = Toast(msg ? msg : this.errors.all()[0])
        setTimeout(() => {
            instance.close();
            this.$set(this, 'loading', false)
        }, 1000);
    }
}